package org.melord.cap;

import jpcap.NetworkInterface;

import org.eclipse.swt.SWT;
import org.eclipse.swt.layout.GridData;
import org.eclipse.swt.layout.GridLayout;
import org.eclipse.swt.widgets.Button;
import org.eclipse.swt.widgets.Combo;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Dialog;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.Group;
import org.eclipse.swt.widgets.Shell;
import org.eclipse.swt.events.SelectionAdapter;
import org.eclipse.swt.events.SelectionEvent;

public class OptionDialog extends Dialog {

	protected Object result;
	protected Shell shell;
	protected int code;

	protected Capture capture;

	protected Cfg cfg = Cfg.getInstance();

	/**
	 * Create the dialog.
	 * 
	 * @param parent
	 * @param style
	 */
	public OptionDialog(Shell parent, int style) {
		super(parent, style);
		setText("SWT Dialog");
	}

	/**
	 * Open the dialog.
	 * 
	 * @return the result
	 */
	public int open() {
		createContents();
		shell.open();
		shell.layout();
		Display display = getParent().getDisplay();
		while (!shell.isDisposed()) {
			if (!display.readAndDispatch()) {
				display.sleep();
			}
		}
		return code;
	}

	/**
	 * Create contents of the dialog.
	 */
	private void createContents() {
		shell = new Shell(getParent(), getStyle());
		shell.setSize(550, 300);
		shell.setText("选项");
		shell.setLayout(new GridLayout(1, false));

		Group group = new Group(shell, SWT.NONE);
		group.setLayoutData(new GridData(SWT.FILL, SWT.CENTER, true, false, 1,
				1));
		group.setText("请选择网卡");
		group.setLayout(new GridLayout(1, false));

		final Combo deviceCombo = new Combo(group, SWT.NONE);
		deviceCombo.setLayoutData(new GridData(SWT.FILL, SWT.CENTER, true,
				false, 1, 1));
		final NetworkInterface[] devices = capture.getDevices();
		int i = 0;
		for (NetworkInterface device : devices) {
			deviceCombo.add(device.description);
			if (device.description.trim().equals(cfg.getDeviceName())) {
				deviceCombo.select(i);
			}
			i++;
		}

		Composite composite = new Composite(shell, SWT.NONE);
		composite.setLayout(new GridLayout(2, true));
		GridData gd_composite = new GridData(SWT.RIGHT, SWT.BOTTOM, true, true,
				1, 1);
		gd_composite.minimumHeight = 35;
		gd_composite.widthHint = 150;
		composite.setLayoutData(gd_composite);

		Button ok = new Button(composite, SWT.NONE);
		ok.addSelectionListener(new SelectionAdapter() {
			@Override
			public void widgetSelected(SelectionEvent e) {
				NetworkInterface device = capture.getDevices()[deviceCombo
						.getSelectionIndex()];
				String name = device.description.trim();
				cfg.setDeviceName(name);
				cfg.save();
				capture.setDevice(device);
				shell.dispose();
			}
		});
		ok.setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, true, 1, 1));
		ok.setText("确定");

		Button cancel = new Button(composite, SWT.NONE);
		cancel.addSelectionListener(new SelectionAdapter() {
			@Override
			public void widgetSelected(SelectionEvent e) {
				shell.dispose();
			}
		});
		cancel
				.setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, true, 1,
						1));
		cancel.setText("取消");

	}

	public void setCapture(Capture capture) {
		this.capture = capture;
	}

}
